package com.neusoft.dibuy.lib.cl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;

public class MyStream1 {
    public static void main(String[] args)
    {
        Random random =new Random();
        List<Dog> dogs =new ArrayList<>();
        IntStream.range(1,11)
                .forEach( index -> {
                    Dog dog = new Dog();
                    dog.name ="Index:" + index;
                    dog.age = random.nextInt(
                            10);
                    dogs.add(dog);
                });
        dogs.stream()
                .forEach( dog -> System.out.println(dog));
//        dogs.stream()
//                .reduce((dog1, dog2) -> {
//                    dog1.age = dog1.age + dog2.age;
//                    return dog1; // 返回的是一个集合的元素类型相同的对象
//                }).ifPresent(dog -> System.out.println(dog));
//         Dog dog =  new Dog();
//         dog.age = 0;
//         dog.name="abc";
//        Dog dog3 = dogs.stream()
//                .reduce(dog, (dog1, dog2) -> {
//                    dog1.age = dog1.age + dog2.age;
//                    return dog1;
//                });
//        System.out.println(dog3);
//        int sum = 0 ;
//        sum = dogs.stream()
//                .reduce(sum, (sum1, dog) -> sum1 + dog.age, (sum1, sum2) -> sum1 + sum2);
//        System.out.println(sum);
            Dog dog =  new Dog();
         dog.age =-1;
         dog.name="abc";
         dog = dogs.stream()
                 .reduce(dog, (dog1, dog2) ->    dog1.age > dog2.age ? dog1 : dog2             );
        System.out.println(dog);
    }
}
class  Dog{
    String name;
    int age;

    @Override
    public String toString() {
        return "Dog{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
